<template>
  <div>
    <div>
      <el-row v-show="!showEdit">
        <el-button type="primary" icon="Plus" @click="handleCreate"
          >新增
        </el-button>
      </el-row>
      <el-row v-show="showEdit">
        <el-button type="primary" icon="Back" @click="backTable"
          >返回
        </el-button>
        <el-button type="success" icon="Check" @click="submitForm"
          >保存
        </el-button>
      </el-row>
    </div>
    <el-divider></el-divider>
    <div v-show="!showEdit">
      <el-form :inline="true">
        <el-form-item label="门店名称">
          <el-input
            v-model="query.branchName"
            placeholder="请输入门店名称或编码"
            @keyup.enter="currentChange(1)"
          ></el-input>
        </el-form-item>
        <el-form-item label="门店状态">
          <el-select
            v-model="query.branchStatus"
            clearable
            placeholder="请选择"
            style="width: 100px"
          >
            <el-option
              v-for="item in branchStatusOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="Search" @click="currentChange(1)"
            >查询</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <div v-show="!showEdit">
      <el-table
        :data="tableData"
        row-key="sysBranchId"
        border
        default-expand-all
        stripe
      >
        <el-table-column type="index" label="序号" width="60">
        </el-table-column>
        <el-table-column
          prop="branchCode"
          label="门店编码"
          sortable
          width="180"
        >
        </el-table-column>
        <el-table-column prop="branchName" label="门店名称"> </el-table-column>
        <el-table-column label="省市区">
          <template #default="scope">
            {{ scope.row.province + '/' + scope.row.city + '/' + scope.row.district }}
          </template>
        </el-table-column>
        <el-table-column prop="branchAddress" show-overflow-tooltip label="详细地址">
        </el-table-column>
        <el-table-column prop="branchTel" label="联系电话"> </el-table-column>
        <el-table-column prop="branchStatus" label="门店状态">
          <template #default="scope">
            <el-switch
              v-model="scope.row.branchStatus"
              active-text="营业"
              inactive-text="闭店"
              :active-value="1"
              :inactive-value="2"
              @change="handleEnabledSysBranch(scope.row)"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template #default="scope">
            <el-button size="small" icon="Edit" @click="handleUpdate(scope.row)"
              >编辑</el-button
            >
            <el-button
              size="small"
              icon="Delete"
              type="danger"
              @click="handleDelete(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-row type="flex" class="row-bg" justify="end">
        <el-pagination
          background
          layout="total, prev, pager, next"
          :current-page="query.current"
          :page-size="query.size"
          :total="query.total"
          @current-change="currentChange"
        >
        </el-pagination>
      </el-row>
    </div>

    <div v-show="showEdit">
      <el-tabs type="border-card">
        <el-tab-pane label="基本信息">
          <el-form
            ref="formValidate"
            :rules="rules"
            :model="form"
            label-width="80px"
          >
            <el-row>
              <el-col :span="5">
                <el-form-item label="门店编码" prop="branchCode">
                  <el-input v-model="form.branchCode"></el-input>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="5">
                <el-form-item label="门店名称" prop="branchName">
                  <el-input v-model="form.branchName"></el-input>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="5">
                <el-form-item label="联系电话" prop="branchTel">
                  <el-input v-model="form.branchTel"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="5">
                <el-form-item label="邮政编码">
                  <el-input v-model="form.postCode"></el-input>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="5">
                <el-form-item label="省市区" prop="branchAddress">
                  <el-cascader
                    v-model="selectedOptions"
                    placeholder="请选择省市区"
                    :options="pcaTextArr"
                    @change="handleChange"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="5">
                <el-form-item label="详细地址" prop="branchAddress">
                  <el-input v-model="form.branchAddress"></el-input>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </el-tab-pane>
      </el-tabs>
    </div>
  </div>
</template>

<script setup name="Branch">
import {
  pageSysBranch,
  addSysBranch,
  updateSysBranch,
  deleteSysBranch,
  enabledSysBranch,
} from "@/api/branch";
import { ElMessageBox } from "element-plus";
import { ElMessage } from "element-plus";
import { ref, onMounted } from "vue";
import { formatTableColumnDate } from "@/utils/dateUtil";
import { pcaTextArr } from "element-china-area-data";

let formValidate = ref();
let showEdit = ref(false);
let query = ref({ size: 10, current: 0, total: 1 });
let form = ref({ branchStatus: 2 });
let tableData = ref([]);
let rules = ref({
  branchCode: [{ required: true, message: "请输入门店编码", trigger: "blur" }],
  branchName: [{ required: true, message: "请输入门店名称", trigger: "blur" }],
  branchTel: [
    { required: true, message: "请输入门店联系电话", trigger: "blur" },
  ],
  branchAddress: [
    { required: true, message: "请输入门店详细地址", trigger: "blur" },
  ],
});
let branchStatusOptions = ref([
  {
    value: 1,
    label: "营业",
  },
  {
    value: 2,
    label: "闭店",
  },
]);
let selectedOptions = ref();

function currentChange(current) {
  query.value.current = current;
  loadData();
}
function loadData() {
  pageSysBranch(query.value)
    .then((res) => {
      query.value.size = res.data.size;
      query.value.current = res.data.current;
      query.value.total = res.data.total;
      tableData.value = res.data.records;
    })
    .catch((err) => {
      console.log(err);
    });
}
function resetForm() {
  form.value = { branchStatus: 2 };
  selectedOptions.value = [];
}
function backTable() {
  resetForm();
  showEdit.value = false;
}
function handleUpdate(row) {
  showEdit.value = true;
  form.value = JSON.parse(JSON.stringify(row));
  selectedOptions.value = [
    form.value.province,
    form.value.city,
    form.value.district,
  ];
}
function handleCreate() {
  showEdit.value = true;
}
function handleDelete(row) {
  ElMessageBox.confirm("此操作将永久删除该记录, 是否继续?", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      deleteSysBranch(row.sysBranchId)
        .then((res) => {
          ElMessage.success("删除成功");
          currentChange(1);
        })
        .catch((err) => {
          console.log(err);
        });
    })
    .catch((err) => {
      ElMessage.message({
        type: "info",
        message: "取消删除",
      });
    });
}
function submitForm() {
  formValidate.value.validate((valid) => {
    if (valid) {
      if (form.value.sysBranchId != null) {
        updateSysBranch(form.value)
          .then((res) => {
            showEdit.value = false;
            resetForm();
            ElMessage.success("修改成功");
            loadData();
          })
          .catch((err) => {
            console.log(err);
          });
      } else {
        addSysBranch(form.value)
          .then((res) => {
            showEdit.value = false;
            resetForm();
            ElMessage.success("保存成功");
            loadData();
          })
          .catch((err) => {
            console.log(err);
          });
      }
    }
  });
}
function handleEnabledSysBranch(row) {
  enabledSysBranch(row.sysBranchId, row.branchStatus)
    .then((res) => {
      ElMessage.success(row.branchStatus == 1 ? "开启营业" : "关闭营业");
    })
    .catch((err) => {
      console.log(err);
    });
}
const handleChange = (value) => {
  form.value.province = value[0];
  form.value.city = value[1];
  form.value.district = value[2];
};

onMounted(() => {
  loadData();
});
</script>

<style>
table {
  width: 100% !important;
}
</style>
